﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace CungMuaC5.Account_Admin.QuanLyTaiKhoan
{
    public partial class EditTaiKhoan : System.Web.UI.Page
    {
        public int MaTK;
        public int ToKnow;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Fill();
            }
        }
        private void Fill()
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan();
            MaTK = int.Parse(Request.QueryString["ma"].ToString());
            List<DTO_TaiKhoan> list = new List<DTO_TaiKhoan>();
            list = bus.XemChiTiet(MaTK);


            switch (list[0].TinhTrang)
            {
                case 0: DropDownList2.SelectedIndex = 0;
                    break;
                case 1: DropDownList2.SelectedIndex = 1;
                    break;

            }
            switch (Convert.ToInt32(list[0].GioiTinh))
            {
                case 0: DropDownList1.SelectedIndex = 1;
                    break;
                case 1: DropDownList1.SelectedIndex = 0;
                    break;

            }
            Label1.Text = list[0].MaTK.ToString();
            TextBox13.Text = list[0].HoTen;
            Label3.Text = list[0].Email;
            Label1.Text = list[0].MaTK.ToString();
            TextBox1.Text = list[0].Password;



            Label37.Text = list[0].NgaySinh.ToString();
            TextBox5.Text = list[0].DienThoai;

            switch (list[0].LoaiTK)
            {
                case 0: Label13.Text = "Người Dùng Thường";
                    break;
                case 1: Label13.Text = "Người Dùng Admin";
                    break;
                case 2: Label13.Text = "Người Dùng Doanh Nghiệp";
                    break;
            }


            List<DTO_DNKV> list1 = new List<DTO_DNKV>();
            list1 = bus.XemChiTietBoSung(MaTK);
            ToKnow = list1.Count;
            if (list1.Count == 0)
            {
                Label15.Visible = false;
                Label21.Visible = false;
                Label22.Visible = false;
                TextBox6.Visible = false;
                Label24.Visible = false;
                TextBox7.Visible = false;
                Label26.Visible = false;
                TextBox8.Visible = false;
                Label27.Visible = false;
                TextBox9.Visible = false;
                Label28.Visible = false;
                TextBox10.Visible = false;
                Label31.Visible = false;
                TextBox11.Visible = false;
                Label33.Visible = false;
                TextBox12.Visible = false;
                Label35.Visible = false;
            }
            else
            {
                Label21.Text = list1[0].MaDN.ToString();
                TextBox6.Text = list1[0].ChucVu;
                TextBox7.Text = list1[0].TenDoanhNghiep;
                TextBox8.Text = list1[0].Website;
                TextBox9.Text = list1[0].DiaChi;
                TextBox10.Text = list1[0].LoaiHinhKinhDoanh.ToString();
                TextBox11.Text = list1[0].NoiMuonQuangCao.ToString();
                TextBox12.Text = list1[0].ThongTinHopTac;
                // Label36.Text = list1[0].TinhTrang.ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan();
            BUS_TaiKhoan bus1 = new BUS_TaiKhoan();
            MaTK = int.Parse(Request.QueryString["ma"].ToString());
            List<DTO_DNKV> list1 = new List<DTO_DNKV>();
            list1 = bus.XemChiTietBoSung(MaTK);
            ToKnow = list1.Count;
            int a = ToKnow;
            int b=0;
            if(a!=0)
            {
                string pass = TextBox1.Text;
                string DienThoai = TextBox5.Text;
                string GT = DropDownList1.SelectedItem.Value;
                int TinhTrang =Convert.ToInt32( DropDownList2.SelectedItem.Value);

                string HoTen = TextBox13.Text;
                int GioiTinh;

                if (GT == "Nữ")
                {
                    GioiTinh = 0;
                }
                else
                {
                    GioiTinh = 1;
                }

                string ChucVu=TextBox6.Text;
                string TenDN=TextBox7.Text;
                string web=TextBox8.Text ;
                string DiaChi=TextBox9.Text;
                string LoaiSP=TextBox10.Text;
                int NoiQC=Convert.ToInt32( TextBox11.Text);
                string thongtin=TextBox12.Text;
                if (TextBox1.Text == "")
                {
                    b = -1;
                    Label38.Visible = true;
                }
                if (b != -1)
                {
                   
                    bool i = bus.update(pass, DienThoai, GioiTinh, MaTK,HoTen,TinhTrang);
                    bool j = bus.updatebosung(ChucVu, TenDN, web, DiaChi, LoaiSP, NoiQC, thongtin, MaTK);
                }
            }
            else
            {
                string pass=TextBox1.Text;
                string DienThoai=TextBox5.Text;    
                string GT=DropDownList1.SelectedItem.Value;
                string HoTen = TextBox13.Text;
                int TinhTrang = Convert.ToInt32(DropDownList2.SelectedItem.Value);
                int GioiTinh;
            
                if(GT=="Nữ")
                {
                    GioiTinh=0;
                }
                else
                {
                    GioiTinh=1;
                }
                if (TextBox1.Text == "")
                {
                    b = -1;
                    Label38.Visible = true;
                }
                if (b != -1)
                {
                    bool i = bus.update(pass, DienThoai, GioiTinh, MaTK,HoTen,TinhTrang);
                }
            }
           
        }
    }
}